'\" te
.\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
.\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH DUP2 3C "February 17, 2023"
.SH NAME
dup2, dup3 \- duplicate an open file descriptor
.SH SYNOPSIS
.nf
#include <unistd.h>

\fBint\fR \fBdup2\fR(\fBint\fR \fIfildes\fR, \fBint\fR \fIfildes2\fR);
.fi

.LP
.nf
\fBint\fR \fBdup3\fR(\fBint\fR \fIfildes\fR, \fBint\fR \fIfildes2\fR, \fBint\fR \fIflags\fR);
.fi

.SH DESCRIPTION
The \fBdup2()\fR function causes the file descriptor \fIfildes2\fR to refer to
the same file as \fIfildes\fR. The \fIfildes\fR argument is a file descriptor
referring to an open file, and \fIfildes2\fR is a non-negative integer less
than the current value for the maximum number of open file descriptors  allowed
the calling process.  See \fBgetrlimit\fR(2). If \fIfildes2\fR already refers
to an open file, not \fIfildes\fR, it is closed first. If \fIfildes2\fR refers
to \fIfildes\fR, or if \fIfildes\fR is not a valid open file descriptor,
\fIfildes2\fR will not be closed first.
.sp
.LP
The \fBdup2()\fR function is equivalent to  \fBfcntl\fR(\fIfildes\fR,
\fBF_DUP2FD\fR, \fIfildes2\fR).
.sp
.LP
The \fBdup3()\fR function works similarly to the \fBdup2()\fR function with
two exceptions.  If \fIfildes\fR and \fIfildes2\fR point to the same file
descriptor, -1 is returned and errno set to \fBEINVAL\fR.  If \fIflags\fR
is \fBO_CLOEXEC\fR, then \fIfiledes2\fR will have the \fBFD_CLOEXEC\fR flag
set causing the file descriptor to be closed during any future call of
\fBexec\fR(2).
.SH RETURN VALUES
Upon successful completion a non-negative integer representing the file
descriptor is returned. Otherwise, \fB\(mi1\fR is returned and \fBerrno\fR is
set to indicate the error.
.SH ERRORS
The \fBdup2()\fR and \fBdup3()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 10n
The \fIfildes\fR argument is not a valid open file descriptor.
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 10n
The \fIfildes2\fR argument is negative or is not less than the current resource
limit returned by \fBgetrlimit(RLIMIT_NOFILE, .\|.\|.)\fR.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 10n
A signal was caught during the \fBdup2()\fR call.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 10n
The process has too many open files.  See  \fBfcntl\fR(2).
.RE

.sp
.LP
Additionally, the \fBdup3()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
\fIflags\fR has a value other than 0 or \fBO_CLOEXEC\fR or \fIfildes\fR and
\fIfildes2\fR point to the same file descriptor.
.RE

.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	Async-Signal-Safe
.TE

.SH SEE ALSO
.BR close (2),
.BR creat (2),
.BR exec (2),
.BR fcntl (2),
.BR getrlimit (2),
.BR open (2),
.BR pipe (2),
.BR attributes (7),
.BR standards (7)
